package com.qiankan.qmsj.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiankan.qmsj.R;
import com.qiankan.qmsj.activity.ProductByTypeActivity;
import com.qiankan.qmsj.utils.DensityUtils;
import com.qiankan.qmsj.widget.TipToast;
import com.xingchen.HorizontalScrollSelectView;
import com.xk.sanjay.rulberview.RulerWheel;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/18.
 */

public class MatchFragment extends BaseFragment {
    @BindView(R.id.tv_accrual)
    TextView mAccrual;
    @BindView(R.id.ruler_view)
    RulerWheel mRulerView;
    @BindView(R.id.select_view)
    HorizontalScrollSelectView selectView;
    @BindView(R.id.tv_month)
    TextView mTvMonth;
    @BindView(R.id.tv_money)
    TextView mTvMoney;
    private int currentMonth = 3;
    private int currentMoney = 0;
    private double monthlyRate = 0.09 / 12;
    private List<String> monthList;

    @Override
    public int getContentViewId() {
        return R.layout.match_fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRulerView();
        initWheelView();
    }

    private void initRulerView() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i <= 300000; i += 1000) {
            list.add(i + "");
        }
        mRulerView.setData(list);
        mRulerView.setSelectedValue("0");
        mTvMoney.setText("0");
        mRulerView.setScrollingListener(new RulerWheel.OnWheelScrollListener() {
            @Override
            public void onChanged(RulerWheel wheel, Object oldValue, Object newValue) {
                currentMoney = Integer.parseInt(newValue + "");
                mTvMoney.setText(newValue + "");
                mAccrual.setText(mothRepays(currentMoney, monthlyRate, currentMonth));
            }

            @Override
            public void onScrollingStarted(RulerWheel wheel) {

            }

            @Override
            public void onScrollingFinished(RulerWheel wheel) {

            }
        });
    }

    private void initWheelView() {
        monthList = new ArrayList<>();
        for (int i = 0; i < 36; i++) {
            monthList.add(i + 1 + "月");
        }
        mTvMonth.setText(monthList.get(2));
        selectView.setAdapter(new MonthAdapter());
        selectView.setMScrollListener(new HorizontalScrollSelectView.OnScrollListener() {
            @Override
            public void onScrollSelectItem(ViewGroup viewGroup, int position) {
                mTvMonth.setText(monthList.get(position));
                currentMonth = position + 1;
                mAccrual.setText(mothRepays(currentMoney, monthlyRate, currentMonth));
            }
        });
        mTvMonth.getLayoutParams().width = DensityUtils.getSecreenWidth(getActivity()) / 5;
    }

    @OnClick(R.id.btn_match)
    public void onClickMatch(View view) {
        if (currentMoney == 0) {
            TipToast.toast(getActivity(), "请选择金额");
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putBoolean("isShowBanner", false);
        bundle.putString("type", "");
        bundle.putString("title", "搜索结果");
        bundle.putString("minTime", currentMonth + "");
        bundle.putString("maxMoney", currentMoney + "");

        startActivity(ProductByTypeActivity.newIntent(getActivity(), bundle));
    }

    /**
     * @param monthlyrate 月利息
     * @param monthnumber 期限
     * @param money       借款金额
     * @return 每月还款金额
     */
    private String mothRepays(double money, double monthlyrate, int monthnumber) {
        double moneys = money * monthlyrate * Math.pow((1 + monthlyrate), monthnumber) / (Math.pow(1 + monthlyrate, monthnumber) - 1);
        return doubleFormateStr(moneys);
    }


    private String doubleFormateStr(double str) {
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(2);
        return nf.format(str);
    }

    private class MonthAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return monthList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tvMonth = null;
            if (convertView == null) {
                LinearLayout root = new LinearLayout(getActivity());
                root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.MATCH_PARENT));
                LinearLayout linearLayout = new LinearLayout(getActivity());
                linearLayout.setLayoutParams(new ViewGroup.LayoutParams(DensityUtils.getSecreenWidth(getActivity()) / 5,
                        DensityUtils.dp2px(getActivity(), 50)));
                linearLayout.setGravity(Gravity.CENTER);
                tvMonth = new TextView(getActivity());
                tvMonth.setTextSize(18);
                tvMonth.setTextColor(Color.parseColor("#999999"));
                tvMonth.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));
                tvMonth.setText(monthList.get(position));
                linearLayout.addView(tvMonth);
                root.addView(linearLayout);
                convertView = root;
                convertView.setTag(tvMonth);
            }
            tvMonth = (TextView) convertView.getTag();
            tvMonth.setText(monthList.get(position));
            return convertView;
        }
    }
}
